//
//  MCTrainTimeVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/19.
//

import UIKit
import Toast_Swift
import QMUIKit



class MCTrainTimeVc: MCBaseVc {
    
    
    var type:Int = 0
    var ids:String = ""
    var planDateArr:Array<String>?
    
    let timeTableViewIdentifier = "timeTableViewIdentifier"
    
    private var HourStr:String = "00"
    
    private var minteStr:String = "00"
    
    @IBOutlet weak var isPlandisPlayLabel: UILabel!
    
    @IBOutlet weak var isPlanChangeTextLabel: UILabel!
    //计划时间加入
    @IBOutlet weak var isPlanTimeLabel: UILabel!
    
    @IBOutlet weak var timePickerView: UIPickerView!
    @IBOutlet weak var timeTableView: UITableView!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
            
        self.view.backgroundColor = UIColor.white
        
        if type == 0 {
            isPlanTimeLabel.isHidden = false
            isPlanChangeTextLabel.text = "设置后将在训练开始前10分钟提醒您"
            isPlanTimeLabel.isHidden = true
            self.timeTableView.isHidden = true
        }else{
            isPlanTimeLabel.isHidden = true
            isPlanTimeLabel.isHidden = false
            self.timeTableView.isHidden = false
        }
        
        //选择器
        timePickerView.dataSource = self
        timePickerView.delegate = self
       
    
        
        timeTableView.dataSource = self
        timeTableView.delegate = self
        timeTableView.rowHeight = 25
        
        timeTableView.register(UINib(nibName: "MCTrainTimeCell", bundle: Bundle.main), forCellReuseIdentifier: timeTableViewIdentifier)
    }
    
    
    
    @IBAction func enterBtn(_ sender: UIButton) {
        if type == 0 {
            self.addPlanApi()
        }else {
            
        }
        
    }
    
    
    func addPlanApi() -> Void {
        
        let TimeInfo = "\(HourStr):\(minteStr):00"
        
        NetWorkRequest(MCAddPlanApi.AddPlanApi(planDate:self.planDateArr, planId: self.ids , planStartTime:TimeInfo)) { ResponseModel in
            self.view.makeToast("添加成功",position: .center)
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5) {
                self.navigationController?.popToRootViewController(animated: true)
            }
            
            
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    

}

extension MCTrainTimeVc:UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MCTrainTimeCell = tableView.dequeueReusableCell(withIdentifier: timeTableViewIdentifier, for: indexPath) as! MCTrainTimeCell
        
        return cell
    }
    
    
}


extension MCTrainTimeVc:UIPickerViewDataSource,UIPickerViewDelegate {
    
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return 24
        }else {
            return 60
        }
        
    }
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return 150
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 56
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        if component == 0 {
            let pickerView = QMUILabel.init()
            pickerView.frame = CGRect(x: 0, y: 0, width: 0, height: 42)
            pickerView.font = UIFont(name: "PingFangSC-Semibold", size: 30)
            pickerView.text = String(format: "%02d", row)
            pickerView.textAlignment = .center
            return pickerView
        }else {
            
            let pickerView = QMUILabel.init()
            pickerView.text = String(format: "%02d", row);
            pickerView.font = UIFont(name: "PingFangSC-Semibold", size: 30)
            pickerView.textAlignment = .center
            pickerView.frame = CGRect(x: 0, y: 0, width: 0, height: 42)
            return pickerView
        }
        
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
       
        if component == 0 {
        
            self.HourStr = "\(String(format: "%02d", row))"
            
        } else {
            self.minteStr = "\(String(format: "%02d", row))"
        }
        
        
        
    }
    
    
}
